/*
 * Copyright (C) 2014 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.camera;

import java.util.ArrayList;
import rpg.RPG;
import rpg.entities.Entity;
import rpg.gfx.Bitmap;
import rpg.maps.Map;

/**
 *
 * @author Rothens
 */
public class Camera {

    private int x;
    private int y;
    private Map actualMap;
    private Entity followed;
    private ArrayList<Cinematic> cinematics = new ArrayList<Cinematic>();
    

    public Camera(int x, int y, Map map) {
        this.x = x;
        this.y = y;
        actualMap = map;
        update();
    }

    public Camera(Entity followed, Map map) {
        this.followed = followed;
        actualMap = map;
        update();

//        movement.add(new CMJumpto(0, 0, this));
//        movement.add(new CMGoto(followed, 0.1f, this));
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Entity getFollowed() {
        return followed;
    }

    public void setFollowed(Entity followed) {
        this.followed = followed;
    }

    public void update() {
        if(!cinematics.isEmpty()){
            Cinematic cm = cinematics.get(0);
            if(cm.tick()){
                cinematics.remove(0);
            }
        } else if (followed != null) {
            x = followed.getCurrentLocation().getX();
            y = followed.getCurrentLocation().getY();
        }
        correctView();
    }
    
    public void addCinematic(Cinematic c){
        cinematics.add(c);
    }

    public void setActualMap(Map actualMap) {
        this.actualMap = actualMap;
    }

    public Map getActualMap() {
        return actualMap;
    }
    
    public Bitmap getCinematicOverlay(){
        if(cinematics.isEmpty()) return null;
        return cinematics.get(0).getEffect();
    }
    
    

    public void correctView() {
        int hw = RPG.WINDOW_WIDTH / 2;
        int hh = RPG.WINDOW_HEIGHT / 2;
        if (x < hw) {
            x = hw;
        } else if (x > actualMap.getWidth() * 64 - hw) {
            x = actualMap.getWidth() * 64 - hw;
        }

        if (y < hh) {
            y = hh;
        } else if (y > actualMap.getHeight() * 64 - hh) {
            y = actualMap.getHeight() * 64 - hh;
        }
    }
}
